﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace UCF.Core
{
    /// <summary>
    /// DllPoolManager
    /// </summary>
    /// <remarks>2013-12-26 by sutroon</remarks>
    public class DllPoolManager
    {
        public List<string> Message { get; set; }
        public object CreateInstance(string formname, string assemblyname = "", Dictionary<string, object> properties = null)
        {
            if (formname.Contains('.'))
            {
                if (assemblyname.Length == 0) assemblyname = formname.Substring(0, formname.LastIndexOf('.'));
            }
            else
            {
                formname = assemblyname + "." + formname;
            }
            Assembly ass = Assembly.Load(assemblyname);
            Type type = ass.GetType(formname);
            
            object frm = Activator.CreateInstance(type);
            this.Message = new List<string>();
            if (properties != null)
            {
                foreach (string n in properties.Keys)
                {
                    PropertyInfo pi = type.GetProperty(n);
                    if (pi == null)
                    {
                        this.Message.Add("成员 [{0}] null".Fmt(n));
                    }
                    else
                    {
                        pi.SetValue(frm, properties[n], null);
                    }
                }
            }
            return frm;
        }
    }
}
